//
//  WelcomeViewController.swift
//  sinaWeiBo
//
//  Created by weixq on 15/8/5.
//  Copyright © 2015年 weixq. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    private var iconButtomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareUI()
     
        if let urlString = UserAccount.loadAccount()?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: urlString))
        }
        
    }
    // MARK: - 加载头像动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 修改底部约束
        iconButtomCons?.constant = -UIScreen.mainScreen().bounds.height - iconButtomCons!.constant
        
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
                // 强制跟新约束
                self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                // 发送通知，切换控制器
                NSNotificationCenter.defaultCenter().postNotificationName(HMRootViewControllerSwitchNotification, object: true)
        }

    }
    
    
    
    func  prepareUI(){
        // MARK: - 准备界面
        // 加载控件
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
        
        // 自动化布局
        // 1> 背景图片
        backImageView.ff_Fill(view)
        
        // 2> 头像
        let conts = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -160))
        
        // 记录底边约束
        iconButtomCons = iconView.ff_Constraint(conts, attribute: NSLayoutAttribute.Bottom)
        
        // 3> 文本标签
        label.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
    }
    
    // MARK: - 懒加载控件
    //背景图像
    private lazy var  backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))

    //用户头像
    private lazy var iconView: UIImageView = {
        
        var ui = UIImageView(image: UIImage(named: "avatar_default_big"))
            ui.layer.masksToBounds = true
            ui.layer.cornerRadius = 45
            return ui
        }()
    
    // 欢迎lable
    private lazy var label:UILabel = {
        var ui = UILabel()
            ui.text = "欢迎归来"
            ui.sizeToFit()
        return ui
        }()
}
